package com.blb.demo2;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 聊天客户端
 */
public class ChatClient {

    public static final int THREADS=2;

    public void connect(String ip,int port){

        int cpunum = Runtime.getRuntime().availableProcessors();
        ExecutorService executorService = new ThreadPoolExecutor(
                cpunum * THREADS,
                cpunum * THREADS,
                0,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
        try{
            Socket socket = new Socket(ip, port);
            executorService.execute(new WriteRunnable(new ObjectOutputStream(socket.getOutputStream())));
            executorService.execute(new ReadRunnable(new ObjectInputStream(socket.getInputStream())));
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        new ChatClient().connect("192.168.1.125",8888);
    }

}
